<?php
define('Acan_Afun_cache',1);
/*
 * cache function
 * 序列化储存
 */
if(Acan_cache_memcache == '1'){
	define('Alang_cache_nowfun','memcache');
	/*
	 * memcache cache function
	 */
	function Afun_memcache_connect(){
		if(function_exists('memcache_init')){
			$Amem = memcache_init();
		}else{
			$Amem = new Memcache;
			$Amem->pconnect(Acan_memcache_server, Acan_memcache_port);
		}
		return $Amem;
	}
	$Amem = Afun_memcache_connect();
	function Afun_cache_set($key,$value=null,$ttl=0){
		global $Amem;
		return $Amem->set(Acan_cache_pre.$key,$value,0,$ttl);
	}
	//读取keys
	function Afun_cache_get($key){
		if(defined('Acache_no')){return;}
		global $Amem;
		return $Amem->get(Acan_cache_pre.$key);
	}
	//删除keys
	function Afun_cache_rm($key){
		global $Amem;
		return $Amem->delete(Acan_cache_pre.$key);
	}
	//删除keys
	function Afun_cache_rmAll(){
		global $Amem;
		return $Amem->flush();
	}
}elseif(Acan_cache_eaccelerator == '1'){
	define('Alang_cache_nowfun','eaccelerator');
	/*
	 * eaccelerator cache function
	 * 序列化储存
	 */
	function Afun_cache_set($key,$value=null,$ttl=0){
		if(!is_array($key)){
			if(function_exists('eaccelerator_get'))
				return eaccelerator_put(Acan_cache_pre.$key,serialize($value),$ttl);
			else
				return;
		}else{
			foreach($key as $a=>$v){
				if(function_exists('eaccelerator_get'))
					eaccelerator_put(Acan_cache_pre.$a,serialize($v['val']),$v['ttl']);
				else
					return;
			}
			return 1;
		}
	}
	//读取keys
	function Afun_cache_get($key){
		if(function_exists('eaccelerator_get'))
			return unserialize(eaccelerator_get(Acan_cache_pre.$key));
		else
			return ;
	}
	//删除keys
	function Afun_cache_rm($key){
		if(function_exists(eaccelerator_rm))
			return eaccelerator_rm(Acan_cache_pre.$key);
		else
			return ;
	}
	//删除过期的keys (session data and content)
	function Afun_cache_gc($key){
		if(function_exists(eaccelerator_gc))
			return eaccelerator_gc(Acan_cache_pre.$key);
		else
			return ;
	}
	//锁定
	function Afun_cache_lock($key){
		if(function_exists(eaccelerator_lock))
			return eaccelerator_lock(Acan_cache_pre.$key);
		else
			return ;
	}
	//解锁
	function Afun_cache_unlock($key){
		if(function_exists(eaccelerator_unlock))
			return eaccelerator_unlock(Acan_cache_pre.$key);
		else
			return ;
	}
}else{
	define('Alang_cache_nowfun','文本缓存');
	/*
	 * file cache function
	 * 序列化储存
	 */
	function Afun_cache_set($key,$data=null,$ttl=0){
		return Afun_filecache_set($key,$data=null,$ttl=0);
	}
	//读取keys
	function Afun_cache_get($key){
		return Afun_filecache_get($key);
	}
	//删除keys
	function Afun_cache_rm($key){
		return Afun_filecache_rm($key);
	}
	//删除keys
	function Afun_cache_rmAll(){
		return Afun_filecache_rmAll();
	}
	//删除过期的keys (session data and content)
	function Afun_cache_gc($key){
		return ;
	}
	//锁定
	function Afun_cache_lock($key){
		return ;
	}
	//解锁
	function Afun_cache_unlock($key){
		return ;
	}
}



function Afun_filecache_set($key,$data=null,$ttl=0,$path=''){
	if(empty($path)) $path = Acan_cache_file_path;
	if(!is_array($key)){
		$name = Acan_cache_pre.$key;
		@header("Content-Type: text/html; charset=utf-8");
		$savefile = $path.$name.'.bin';
		if(!is_dir(dirname($savefile)))
			Afun_file_mkdirm(dirname($savefile));
		//失效时间
		if($ttl > 0){
			$distime = time()+$ttl;
			file_put_contents($savefile.'time',$distime);
		}
		$data = serialize($data);
		file_put_contents($savefile,$data);
		return true;
	}else{
		foreach($key as $a=>$v){
			return Afun_cache_set(Acan_cache_pre.$a,$v['val'],$v['ttl']);
		}
		return 1;
	}
}
function Afun_filecache_get($key,$path=''){
	if(empty($path)) $path = Acan_cache_file_path;
	$name= Acan_cache_pre.$key;
	$savefile = $path.$name.'.bin';
	//--判断失效时间
	if(is_file($savefile.'time')){
		$distime = file_get_contents($savefile.'time');
		if($distime < time()){
			Afun_cache_rm($name);
			return false;//缓存文件失效
		}
	}
	//--获取内容
	if(!is_file($savefile)){
		return false;//缓存文件不存在
	}
	$data = unserialize(file_get_contents($savefile));
	return $data;
}
function Afun_filecache_rm($key,$path=''){
	if(empty($path)) $path = Acan_cache_file_path;
	$savefile = $path.Acan_cache_pre.$key.'.bin';
	@unlink($savefile);
	@unlink($savefile.'time');
	return true;
}
function Afun_filecache_rmAll($path=''){
	if(empty($path)) $path = Acan_cache_file_path;
	Afun_file_rmdirm($path);
	return true;
}

?>